package com.herokuapp.zhachuang.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import lombok.Data;

/**
 * 用户资料
 *
 * @author chazhuang@gmail.com
 */
@Data
@Entity
@SuppressWarnings("PersistenceUnitPresent")
@Table(
	name = "user_profile",
	uniqueConstraints = {
		@UniqueConstraint(
			columnNames = {
				"cellular"
			}
		),
		@UniqueConstraint(
			columnNames = {
				"user_id"
			}
		)
	}
)
@JsonIdentityInfo(
	generator = ObjectIdGenerators.PropertyGenerator.class,
	property = "id"
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserProfile implements Serializable {

	private static final long serialVersionUID = -5340972239061061354L;

	/**
	 * 主键
	 */
	@Basic(optional = false)
	@Column(name = "id", nullable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	private Long id;

	/**
	 * 手机号
	 */
	@Column(name = "cellular")
	private String cellular;

	/**
	 * LINE user ID
	 */
	@Column(name = "user_id")
	private String userId;

	/**
	 * LINE user's display name
	 */
	@Column(name = "display_name")
	private String displayName;

	/**
	 * LINE user's language
	 */
	@Column(name = "\"language\"")
	private String language;

	/**
	 * LINE user's profile image URL
	 */
	@Column(name = "picture_url")
	private String pictureUrl;

	/**
	 * LINE user's status message
	 */
	@Column(name = "status_message")
	private String statusMessage;

	public UserProfile() {
	}

	protected UserProfile(Long id) {
		this.id = id;
	}

	@Override
	public String toString() {
		try {
			return new JsonMapper().writeValueAsString(this);
		} catch (JsonProcessingException ignore) {
			return Objects.isNull(id) ? "null" : id.toString();
		}
	}
}
